package in

import (
	"encoding/json"
	"io/ioutil"
	"log"

	"github.com/gogf/gf/net/ghttp"
)

//***********************************************分界线***********************************************//

//返回自定义结构
func write(r *ghttp.Request, code int, v interface{}) {
	r.Response.WriteHeader(code)
	r.Response.WriteJsonExit(v)
}

//将v转换成json数据并写入到w中。code为服务端返回的代码。
func writeJson(r *ghttp.Request, code int, v interface{}) {
	setHeadJson(r)
	r.Response.WriteHeader(code)
	if err := r.Response.WriteJsonExit(v); err != nil {
		log.Println("writeJson err:", err)
	}
}

//设置head为json格式
func setHeadJson(r *ghttp.Request) {
	r.Header.Set("Content-Type", "application/json;charset=utf-8")
}

//整理数据,json格式
func dealDataJson(code bool, data interface{}, count ...int64) string {
	bs, _ := json.Marshal(dealData(code, data, count...))
	return string(bs)
}

//整理数据,存到map,数量可选填,所有方法根源
func dealData(code bool, data interface{}, count ...int64) interface{} {
	return fnDealData(code, data, count...)
}

//读取json数据存入interface中
func readJson(r *ghttp.Request, v interface{}) {
	data, err := ioutil.ReadAll(r.Body)
	CheckErr(err)
	err = json.Unmarshal(data, v)
	if err != nil {
		Json415(r, err)
	}
}
